#!/usr/bin/env python
"""
Creates a simple hangman game.

Ian Mahoney - 4/9/2010
"""

import random

word_list = ['thyme', 'svelt', 'karaoke', 'autopsy', 'gratuitous', 'allowed', 'trumpet']
tries = 6

class Game:
    def __init__(self, word, tries):
        self.word = word
        self.tries = tries
        self.displayed = []
        self.letters = map(chr, range(ord('a'), ord('z')+1))
        
        for i in range(len(word)):
            self.displayed.append('_')

def choose_word(word_list):
    word = random.choice(word_list)
    return word

def play_turn(game):
    display = ''
    for letter in game.displayed:
        display += letter
    print display
    print 'remaining letters: ' + str(game.letters)
    print 'remaining tries: ' + str(game.tries)
    
    guess = player_guess(game.letters)
    check_guess(game, guess)

def check_guess(game, guess):
    correct = 0
    checking = -1
    for letter in game.word:
        checking += 1
        if letter == guess:
            correct += 1
            game.displayed[checking] = str(guess)
    game.letters.remove(str(guess))
    print
    print str(correct)+' found.'
    if correct < 1:
        game.tries -= 1
            

def player_guess(letters):
    guess = raw_input('enter your guess: ')
    while guess[0] not in letters:
        print 'Invalid guess.  Please enter a letter from the lest of remaining letters.'
        guess = raw_input('enter your guess: ')
    return guess[0]

def main():
    word = str(choose_word(word_list))
    game = Game(word, tries)
    while ('_' in game.displayed) and (game.tries > 0):
        play_turn(game)
    
    if game.tries > 0:
        print 'You Win!  The word is '+str(game.word)
    else:
        print 'You Lose!  The word is '+str(game.word)

main()